package com.axdoc.doctor.service.impl;

import javax.management.RuntimeErrorException;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

import org.springframework.stereotype.Service;

import com.alibaba.dubbo.rpc.RpcException;
import com.axdoc.doctor.entity.User;
import com.axdoc.doctor.exceptions.DoctorBizException;
import com.axdoc.doctor.service.UserService;

/**
 * @author yingjun http://10.26.210.90:8080/user/getUserByPhone/18888888888
 */

@Service("userService")
@Path("user")
@Produces({ "application/json; charset=UTF-8", "text/xml; charset=UTF-8" })
public class UserServiceImpl implements UserService {

	@GET
	@Path("/getUserByPhone/")
	public User getUserByPhone(@QueryParam("phone") String phone) throws DoctorBizException,Exception {
		// try {
		// //测试超时
		// Thread.sleep(1000);
		// } catch (InterruptedException e) {
		// e.printStackTrace();
		// }
		User user;
		user = new User(11L, "ByPhone", phone);
		System.out.println("###########");
		if ("1".equals(phone)) {
			throw new DoctorBizException(DoctorBizException.ERROR, "手机号不存在");
		}
		if ("2".equals(phone)) {
			throw new RuntimeErrorException(new Error("not exsit"), "手机号不存在");
		}
		int a = 5 / 0;
		return user;
	}

	@GET
	@Path("/getUserByName/{name}/")
	public User getUserByName(@PathParam("name") String name) throws DoctorBizException {
		User user = new User(11L, name, "18888888888");
		return user;
	}

	@POST
	@Path("/addUser/")
	@Consumes({ MediaType.APPLICATION_JSON })
	public User addUser(User user) throws DoctorBizException {
		return user;
	}

	@GET
	@Path("/getUserById/{id}/")
	public User getUserById(@PathParam("id") Long id) throws DoctorBizException {
		User user = new User(id, "ByPhone", "123456");
		return user;
	}
}
